import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

class StoragePreferences {
  static final String keyToken = 'key_token';
  static final String keyLocale = 'key_locale';

  static final sp = Get.find<SharedPreferences>();

  static void saveToken(String token) {
    sp.setString(keyToken, token);
  }

  static String? getToken() {
    try {
      var token = sp.getString(keyToken);
      if (token != null) {
        return token;
      }
    } catch (e) {
      debugPrint(e.toString());
      return null;
    }
  }

  static void saveLocale(int index) {
    sp.setInt(keyLocale, index);
  }

  static int? getLocale() {
    try {
      var i = sp.getInt(keyLocale);
      if (i != null) {
        return i;
      }
    } catch (e) {
      return null;
    }
  }

  static void clearToken() {
    var isExist = sp.containsKey(keyToken);
    if (isExist) {
      sp.remove(keyToken);
    }
  }
}
